---
title: ""
output: pdf_document
geometry: "top=1in, bottom=1in, left=1in, right=1in"
header-includes:
    - \usepackage{caption}
    - \usepackage{lscape}
---

\newcommand{\blandscape}{\begin{landscape}}
\newcommand{\elandscape}{\end{landscape}}

\captionsetup[table]{labelformat=empty}

```{r setup, include=FALSE, message=FALSE}

# Setup
if (!require("pacman")) install.packages("pacman", repos = "http://cran.us.r-project.org"); library(pacman);
pacman::p_load(here, glue, stringr, magrittr, dplyr, tidyr, readr, yaml, kableExtra)

# Working directory and command line argument setup
current_script <- "generate_tables_and_figures/gen_outcome_by_subgroup_outputs/src/gen_outcome_by_subgroup_outputs.Rmd"
here::i_am(current_script)
source(here("R", "project_functions.R"))
task_dir <- here(dirname(dirname(file.path(current_script))))
knitr::opts_knit$set(root.dir = task_dir)
cl_args <- parse_make_args(c(
  "SUBGROUP_MODELS",
  "SUBGROUP_FWER",
  "INDEX_SUBGROUP_FWER",
  "CONFIG_FILE",
  "OUT_DIR"
), inter_active = TRUE, task_dir = task_dir)

knitr_setup()

```

```{r}

source('R/outcome_by_subgroup_outputs_functions.R')

config <- read_yaml(cl_args$CONFIG_FILE)

subgroup_models <- read_csv(cl_args$SUBGROUP_MODELS) %>% 
  select(-c(window, covariate_set)) %>% 
  separate(subgroup, c("subgroup_type", "subgroup"), sep = "__") %>% 
  mutate(subgroup_type = str_remove(subgroup_type, "_exp")) %>%
  filter(outcome %in% config$outcomes)

subgroup_fwer <- bind_rows(
  read_csv(cl_args$SUBGROUP_FWER),
  read_csv(cl_args$INDEX_SUBGROUP_FWER)) %>% 
  select(-c(window, covariate_set)) %>% 
  rename(subgroup = subgroup_value) %>% 
  mutate(subgroup_type = if_else(subgroup_type == "risk_exp", "risk", subgroup_type))

source('../R/table_functions.R')
table_config <- read_yaml('../hand/tables_config.yaml')

timestamp <- get_timestamp()
```

\begin{landscape}

```{r}
(pathway <- print_outcomes_by_subgroup_table("pathway", subgroup_models, subgroup_fwer, config))
writeLines(pathway, glue(cl_args$OUT_DIR, "/outcomes_subgroup_pathway_{timestamp}.tex"))
writeLines(pathway, glue(cl_args$OUT_DIR, "/outcomes_subgroup_pathway_up_to_date.tex"))

```

\newpage

```{r}
(nbhood <- print_outcomes_by_subgroup_table("neighborhood", subgroup_models, subgroup_fwer, config))
writeLines(nbhood, glue(cl_args$OUT_DIR, "/outcomes_subgroup_neighborhood_{timestamp}.tex"))
writeLines(nbhood, glue(cl_args$OUT_DIR, "/outcomes_subgroup_neighborhood_up_to_date.tex"))

```

\newpage

```{r}
(risk <- print_outcomes_by_subgroup_table("risk", subgroup_models, subgroup_fwer, config))
writeLines(risk, glue(cl_args$OUT_DIR, "/outcomes_subgroup_risk_{timestamp}.tex"))
writeLines(risk, glue(cl_args$OUT_DIR, "/outcomes_subgroup_risk_up_to_date.tex"))
```

\newpage

```{r}
(age <- print_outcomes_by_subgroup_table("age", subgroup_models, subgroup_fwer, config))
writeLines(age, glue(cl_args$OUT_DIR, "/outcomes_subgroup_age_{timestamp}.tex"))
writeLines(age, glue(cl_args$OUT_DIR, "/outcomes_subgroup_age_up_to_date.tex"))
```

\newpage

```{r}
(table <- subgroup_models %>% 
   filter(outcome == "i_three_components") %>% 
   select(subgroup_type, p_subgroup_ftest_itt) %>% 
   distinct() %>% 
   mutate(subgroup_type = factor(subgroup_type, levels = c("pathway", "neighborhood", "age", "risk")),
          label = case_when(subgroup_type == "pathway" ~ "Primary Index across Pathways",
                            subgroup_type == "neighborhood" ~ "Primary Index across Neighborhoods",
                            subgroup_type == "age" ~ "Primary Index across Age Groups",
                            subgroup_type == "risk" ~ "Primary Index across Risk Groups")) %>% 
   fmt_col("p_subgroup_ftest_itt") %>% 
   arrange(subgroup_type) %>% 
   select(label, p_subgroup_ftest_itt) %>% 
   print_outcomes_by_subgroup_table("ftest", ., subgroup_fwer, config))
writeLines(table, glue(cl_args$OUT_DIR, "/outcomes_subgroup_ftests_{timestamp}.tex"))
writeLines(table, glue(cl_args$OUT_DIR, "/outcomes_subgroup_ftests_up_to_date.tex"))
```

```{r}
(table <- subgroup_models %>%
   select(subgroup_type, outcome, p_subgroup_ftest_itt) %>% 
   distinct() %>% 
   mutate(subgroup_type = factor(subgroup_type, levels = c("pathway", "neighborhood", "age", "risk"))) %>%
   mutate(outcome = factor(outcome, levels = c(
     "i_three_components", "v_shooting_or_homicide", "a_shooting_or_homicide", "a_partone_non_shooting_or_homicide"))) %>% 
   fmt_col("p_subgroup_ftest_itt") %>% 
   arrange(subgroup_type, outcome) %>%
   print_outcomes_by_subgroup_table("all_ftest", ., subgroup_fwer, config))
writeLines(table, glue(cl_args$OUT_DIR, "/all_outcomes_subgroup_ftests_{timestamp}.tex"))
writeLines(table, glue(cl_args$OUT_DIR, "/all_outcomes_subgroup_ftests_up_to_date.tex"))
```

\end{landscape}
